package com.winit.redis.web;

import com.winit.redis.service.RedisService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created by on 2017/3/1.
 */
@RestController
@Api(value = "redis", tags = {"redis"}, description = "redis")
@RequestMapping(value = "/api/v1/redis")
public class RedisController {

    @Autowired
    RedisService redisService;

    /**
     * 设置Str缓存
     * @param key
     * @param val
     * @return
     */
    @RequestMapping(value = "/setStr", method = RequestMethod.GET)
    public String setStr(@ApiParam(value = "val") @RequestParam(value ="val", required = false) String val,
                         @ApiParam(value = "key") @RequestParam(value ="key", required = false) String key
                         ){
        try {
            redisService.setStr(key, val);
            return "success";
        } catch (Exception e){
            e.printStackTrace();
            return "error";
        }
    }

    /**
     * 根据key查询Str缓存
     * @param key
     * @return
     */
    @RequestMapping(value = "getStr",method = RequestMethod.GET)
    public String getStr(@ApiParam(value = "key") @RequestParam(value ="key", required = false) String key){
        return redisService.getStr(key);
    }


    /**
     * 根据key产出Str缓存
     * @param key
     * @return
     */
    @RequestMapping(value = "delStr", method = RequestMethod.GET)
    public String delStr(@ApiParam(value = "key") @RequestParam(value ="key", required = false) String key){
        try {
            redisService.del(key);
            return "success";
        } catch (Exception e){
            return "error";
        }
    }

    /**
     * 获取obj缓存
     * @param key
     * @return
     */
    @RequestMapping(value = "getObj", method = RequestMethod.GET)
    public Object getObj(@ApiParam(value = "key") @RequestParam(value ="key", required = false) String key){
        return redisService.getObj(key);
    }


    /**
     * 删除obj缓存
     * @param key
     * @return
     */
    @RequestMapping(value = "delObj", method = RequestMethod.GET)
    public Object delObj(@ApiParam(value = "key") @RequestParam(value ="key", required = false) String key){
        try {
            redisService.delObj(key);
            return "success";
        } catch (Exception e){
            e.printStackTrace();
            return "error";
        }
    }
}